package handler.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import handler.dataPackage.DriverDeviceMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author dfj@99chuxing.com
 * @date 2018/4/4 0004 12:53
 * 服务端解码器
 */
public class ServerDecoder extends ByteToMessageDecoder{

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("服务端解码器对入站数据进行解码------");
        int i = byteBuf.readableBytes();
        if (i<=0){
            return;
        }
        byte[] bytes =new byte[i];
        byteBuf.readBytes(bytes);
        DriverDeviceMessage driverDeviceMessage = JSON.parseObject(new String(bytes),DriverDeviceMessage.class);
        list.add(driverDeviceMessage);
    }
}
